package com.sky.controller.user;

import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sky.constant.RedisKeyConstant;
import com.sky.constant.StatusConstant;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.DishFlavorService;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@RestController("userDishController")
@RequestMapping("/user/dish")
@Slf4j
@Api(tags = "C端-菜品浏览接口")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    @Cacheable(cacheNames = RedisKeyConstant.CACHE_DISH_BY_CATEGORYID+"#3600",key = "#categoryId")
    public Result<List<DishVO>> list(Long categoryId) {
        //先从redis中查询数据
        List<DishVO> dishCacheList = (List<DishVO>) redisTemplate.opsForValue().get(RedisKeyConstant.CACHE_DISH_BY_CATEGORYID +":"+ categoryId);
        if (dishCacheList != null && dishCacheList.size() > 0){
            return Result.success(dishCacheList);
        }
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getStatus,StatusConstant.ENABLE).eq(Dish::getCategoryId,categoryId);
        List<Dish> dishList = dishService.list(dishLambdaQueryWrapper);
        List<DishVO> dishVOList = new ArrayList<>(dishList.size());
        dishVOList = dishList.stream().map(dish -> {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish,dishVO);
            Category category = categoryService.getById(dish.getCategoryId());
            dishVO.setCategoryName(category.getName());
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishVO.getId());
            List<DishFlavor> dishFlavorList = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
            dishVO.setFlavors(dishFlavorList);
            return dishVO;
        }).collect(Collectors.toList());
//        redisTemplate.opsForValue().set(RedisKeyConstant.CACHE_DISH_BY_CATEGORYID+":"+categoryId,dishVOList,5, TimeUnit.MINUTES);
        return Result.success(dishVOList);
    }

}
